package ar.edu.uba.fi.jmit.graphvisualizer.client;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SecondaryInformationPanel extends VerticalPanel {
	
	private SecondaryInformationServiceAsync secondaryInformationService = GWT.create(SecondaryInformationService.class);
	private FlexTable grid;
	
	@Override
	protected void onLoad() {
		this.add(this.generateBodyPanel());
	}
	
	private Widget generateBodyPanel() {
		grid = new FlexTable();
		Panel panel = new SimplePanel();
		panel.add(grid);
		grid.setBorderWidth(1);
		return panel;
	}
	
	private void addRow(FlexTable flexTable, String key, String value){
		int numRows = flexTable.getRowCount();
		flexTable.setText(numRows, 0, key);
		flexTable.setText(numRows, 1, value);
	}
	
	public void update(String appName, String monName) {
		secondaryInformationService.getSecondaryInformation(appName, monName, new AsyncCallback<Map<String, String>>() {
			@Override
			public void onSuccess(Map<String, String> info) {
				grid.removeAllRows();
				if (info != null) {
					for (String key : info.keySet()) {
						addRow(grid, key, info.get(key));
					}
					setVisible(true);
				}else{
					//grid.setText(0, 0, "No secondary information available for this monitor.");
					setVisible(false);
				}
			}

			@Override
			public void onFailure(Throwable arg0) {
				GWT.log("Failure SecondaryInformationPanel:update");
			}
		});
	}

}
